﻿using System;
using System.Windows.Forms;

namespace UtfprLibrary.Windows.Forms.ComponentModel {
    /// <summary>
    /// TODO
    /// </summary>
    internal partial class UtfprFormValidatableControlCollectionEditor : Form {
        #region Properties

        private UBNValidatableControlCollection _validatableCollection;
        /// <summary>
        /// TODO
        /// </summary>
        public UBNValidatableControlCollection ValidatableCollection {
            get { return _validatableCollection; }
            set { _validatableCollection = value; }
        }

        private UBNValidatableControlCollection _sourceCollection;
        /// <summary>
        /// TODO
        /// </summary>
        public UBNValidatableControlCollection SourceCollection {
            get { return _sourceCollection; }
            set { _sourceCollection = value; }
        }

        #endregion

        /// <summary>
        /// TODO
        /// </summary>
        public UtfprFormValidatableControlCollectionEditor() {
            InitializeComponent();

            _sourceCollection = new UBNValidatableControlCollection();
            _validatableCollection = new UBNValidatableControlCollection();
        }

        #region EventHandlers

        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UtfprFormValidatableControlCollectionEditor_Load(object sender, EventArgs e) {
            _bsSourceCollection.DataSource = _sourceCollection;
            _bsValidatableCollection.DataSource = _validatableCollection;
        }

        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lstSourceCollection_SelectedIndexChanged(object sender, EventArgs e) {
            _pgdPropertyGrid.SelectedObject = _lbxSourceCollection.SelectedItem;
        }

        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lstValidatableCollection_SelectedIndexChanged(object sender, EventArgs e) {
            _pgdPropertyGrid.SelectedObject = _lbxValidatableCollection.SelectedItem;
        }

        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e) {
            _sourceCollection = _validatableCollection;
        }

        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddAll_Click(object sender, EventArgs e) {
            foreach (UtfprValidatableControl i in _sourceCollection) {
                _bsValidatableCollection.Add(i);
            }
            _bsSourceCollection.Clear();
        }

        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRemoveAll_Click(object sender, EventArgs e) {
            foreach (UtfprValidatableControl i in _validatableCollection) {
                _bsSourceCollection.Add(i);
            }
            _bsValidatableCollection.Clear();
        }

        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e) {
            try {
                _bsValidatableCollection.Add(_lbxSourceCollection.SelectedItem);
                _bsSourceCollection.Remove(_lbxSourceCollection.SelectedItem);
            } catch (Exception) { }
        }

        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRemove_Click(object sender, EventArgs e) {
            try {
                _bsSourceCollection.Add(_lbxValidatableCollection.SelectedItem);
                _bsValidatableCollection.Remove(_lbxValidatableCollection.SelectedItem);
            } catch (Exception) { }
        }

        #endregion
    }
}
